/*
 * userManager.java
 *
 * Created on July 13, 2007, 4:25 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.twistinjava.logonDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.twistinjava.exception.TwistinjavaException;
import com.twistinjava.logon.User;
import com.twistinjava.util.DBManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
 * 
 * @author skumar
 */
public class userManager
{
    private static Log log = LogFactory.getLog(userManager.class);
	private String loginID;
	private String password;

	/** Creates a new instance of userManager */
	public userManager()
	{
	}

	public User authenticateUser() throws Exception
	{
            log.debug("inside authenticateUser method of UserManager class **********");
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement pStmt = null;
                User user =null;

		try
		{
			conn = DBManager.getConnection();
			pStmt = conn.prepareStatement("select logonID, firstName, lastName, emailID from User where logonID = ? AND password = ? and isActivated = true");

			pStmt.setString(1, this.getLoginID());
			pStmt.setString(2, this.getPassword());

			rs = pStmt.executeQuery();

			if (rs.next())
			{
                            user = new User();
				user.setLogonID(rs.getString("logonID"));
                                user.setFirstName(rs.getString("firstName"));
                                user.setLastName(rs.getString("lastName"));
                                user.setEmail(rs.getString("emailID"));
			}

		} catch (Exception e)
		{
                    log.error("Exception inside authenticateUser method of UserManager class***"+e.getMessage());
			throw new TwistinjavaException(e.getMessage());
		} finally
		{
			if (rs != null)
			{
				rs.close();
			}
			if (pStmt != null)
			{
				pStmt.close();
			}
			if (conn != null)
			{
				conn.close();
			}
		}
		return user;
	}

	public String getLoginID()
	{
		return loginID;
	}

	public void setLoginID(String loginID)
	{
		this.loginID = loginID;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

}
